package com.magus.project.yddemo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.framework.core.webbean.highcharts.Point;
import com.magus.framework.core.webbean.highcharts.Series;
import com.magus.project.yddemo.entity.Indicator;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.entity.IndicatorDetail;
import com.magus.project.yddemo.repository.IndicatorRepository;

@Service
@Transactional(readOnly=true)
public class IndicatorAnalysisService extends BaseService<Indicator, String> {
	
	@Autowired
	private IndicatorRepository indicatorRepository;
	
	@Autowired
	private IndicatorDataService indicatorDataService;
	
	@Autowired
	private IndicatorDetailService indicatorDetailService;

	@Override
	protected BaseRepository<Indicator, String> getBaseRepository() {
		return  indicatorRepository;
	}

	public List<Series> findDay(String indicatorName, Long beginTime) {
		List<Series> seriess = new ArrayList<>();
		
		List<Point> points = new ArrayList<>();
		List<Indicator> indicator = indicatorRepository.findByNameLikeAndDimensionLike("%"+indicatorName+"%", "%day%");
		List<IndicatorDetail> details=indicatorDetailService.findByIndivatorIn(indicator);
		List<IndicatorData> datas=indicatorDataService.findByIndicatorDetailIn(details,beginTime,"day");
		Map<IndicatorDetail, List<IndicatorData>> collect = datas.stream().collect(Collectors.groupingBy(IndicatorData::getIndicatorDetail));
		collect.entrySet().forEach(temp->{
			Series series = new Series();
			temp.getValue().forEach(t->{
				Point point = new Point();
				point.setY(t.getValue());
				point.setX(0L);
				point.setName(t.getIndicatorDetail().getCompany().getName());
				points.add(point);
			});
			series.setData(points);
			series.setName(temp.getKey().getCompany().getName());
			seriess.add(series);
		});
		
		return seriess;
	}
	
}
